package com.ywy.controller.admin;

import com.ywy.config.ApplicationInitialize;
import com.ywy.entity.ResType;
import com.ywy.service.ResTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员资源类别Controller
 *
 * @author ywy
 * @date 2022/9/1 16:50
 */
@Controller
@RequestMapping("admin/resType")
public class AdminResTypeController {
    @Resource
    private ResTypeService resTypeService;

    @Resource
    private ApplicationInitialize initialize;

    /**
     * 跳转到修改资源类别页面
     *
     * @param id
     * @return
     */
    @RequestMapping({"edit", "edit/{id}"})
    @RequiresPermissions(value = {"RES_TYPE_EDIT"})
    public ModelAndView edit(@PathVariable(value = "id", required = false) Integer id) {
        ResType resType;
        if (id != null) {
            resType = resTypeService.getById(id);
        } else {
            resType = new ResType();
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject("resType", resType);
        mav.setViewName("admin/resType/edit");
        return mav;
    }

    /**
     * 分页查询资源类别信息
     *
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions(value = {"RES_TYPE_QUERY"})
    public Map<String, Object> list(Integer page, Integer limit) {
        List<ResType> arcTypeList = resTypeService.list(page, limit);
        Long total = resTypeService.getTotal();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("count", total);
        resultMap.put("data", arcTypeList);
        return resultMap;
    }

    /**
     * 根据id查询资源类别信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("getById")
    @RequiresPermissions(value = {"RES_TYPE_QUERY"})
    public Map<String, Object> getById(Integer id) {
        ResType resType = resTypeService.getById(id);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resType", resType);
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 添加或者修改资源类别信息
     *
     * @param resType
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("save")
    @RequiresPermissions(value = {"RES_TYPE_EDIT"})
    public Map<String, Object> save(ResType resType, HttpServletRequest request) {
        resTypeService.save(resType);
        initialize.loadData(request.getServletContext());

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 删除资源类别信息
     *
     * @param id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions(value = {"RES_TYPE_DELETE"})
    public Map<String, Object> delete(Integer id, HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resTypeService.delete(id);
            resultMap.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("success", false);
        }

        initialize.loadData(request.getServletContext());
        return resultMap;
    }
}
